package com.huaxin.hxmodulesystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaxin.hxmodulesystem.dto.ResponseResult;
import com.huaxin.hxmodulesystem.dto.request.AddPermissionReqDTO;
import com.huaxin.hxmodulesystem.dto.request.UpdatePermissionReqDTO;
import com.huaxin.hxmodulesystem.models.Permission;
import com.huaxin.hxmodulesystem.service.PermissionService;
import com.huaxin.hxmodulesystem.service.RolePermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "权限")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/sys/permission")
public class RolePermissionController {
    private final RolePermissionService rolePermissionService;
    private final PermissionService permissionService;

    /**
     * 新增权限
     *
     * @param in 入参
     * @return
     */
    @Operation(summary = "新增权限", description = "新增权限")
    @PostMapping("/addPermission")
    public ResponseResult<String> addPermission(@RequestBody AddPermissionReqDTO in) {
        return permissionService.addPermission(in);
    }

    /**
     * 修改权限
     *
     * @param in 入参
     * @return
     */
    @Operation(summary = "修改权限", description = "修改权限")
    @PostMapping("/updatePermission")
    public ResponseResult<String> updatePermission(@RequestBody UpdatePermissionReqDTO in) {
        return permissionService.updatePermission(in);
    }

    /**
     * 删除权限
     *
     * @param id 权限ID
     * @return
     */
    @Operation(summary = "删除权限", description = "删除权限")
    @GetMapping("/deletePermission")
    public ResponseResult<String> deletePermission(@Parameter(description = "权限ID", name = "id", required = true) @RequestParam(name = "id", required = true) String id) {
        return permissionService.deletePermission(id);
    }

    /**
     * 查询权限
     *
     * @param id 权限ID
     * @return
     */
    @Operation(summary = "查询权限", description = "查询权限")
    @GetMapping("/queryPermission")
    public ResponseResult<Permission> queryPermission(@Parameter(description = "权限ID", name = "id", required = true) @RequestParam(name = "id", required = true) String id) {
        return permissionService.queryPermission(id);
    }

    /**
     * 查询权限列表
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @param search   搜索条件
     * @return
     */
    @Operation(summary = "查询权限列表", description = "查询权限列表")
    @GetMapping("/queryPermissionList")
    public ResponseResult<Page<Permission>> queryPermissionList(@Parameter(description = "页码", name = "pageNum", required = true) @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                                                @Parameter(description = "每页数量", name = "pageSize", required = true) @RequestParam(name = "pageSize", required = true) Integer pageSize,
                                                                @Parameter(description = "搜索条件", name = "search", required = false) @RequestParam(name = "search", required = false) String search) {
        return permissionService.queryPermissionList(pageNum, pageSize, search);
    }

    /**
     * 添加权限与角色的关系
     *
     * @param permissionId 权限ID
     * @param roleId       角色ID
     * @return
     */
    @Operation(summary = "添加权限与角色的关系", description = "添加权限与角色的关系")
    @GetMapping("/addPermissionRole")
    public ResponseResult<String> addPermissionRole(@Parameter(description = "权限ID", name = "permissionId", required = true) @RequestParam(name = "permissionId", required = true) String permissionId,
                                                    @Parameter(description = "角色ID", name = "roleId", required = true) @RequestParam(name = "roleId", required = true) String roleId) {
        return rolePermissionService.addPermissionRole(permissionId, roleId);
    }

    /**
     * 删除权限与角色的关系
     *
     * @param permissionId 权限ID
     * @param roleId       角色ID
     * @return
     */
    @Operation(summary = "删除权限与角色的关系", description = "删除权限与角色的关系")
    @GetMapping("/deletePermissionRole")
    public ResponseResult<Boolean> deletePermissionRole(@Parameter(description = "权限ID", name = "permissionId", required = true) @RequestParam(name = "permissionId", required = true) String permissionId,
                                                        @Parameter(description = "角色ID", name = "roleId", required = true) @RequestParam(name = "roleId", required = true) String roleId) {
        return ResponseResult.ok(rolePermissionService.deletePermissionRole(permissionId, roleId));
    }

    /**
     * 根据角色id、用户类型、组织id查询权限列表
     *
     * @param roleId 角色ID
     * @return
     */
    @Operation(summary = "根据角色id、用户类型、组织id查询权限列表", description = "根据角色id、用户类型、组织id查询权限列表")
    @GetMapping("/queryPermissionListByRoleId")
    public ResponseResult<List<Permission>> queryPermissionListByRoleId(@Parameter(description = "角色ID", name = "roleId", required = true) @RequestParam(name = "roleId", required = true) String roleId,
                                                                        @Parameter(description = "用户类型", name = "userType", required = true) @RequestParam(name = "userType", required = true) Integer userType,
                                                                        @Parameter(description = "组织id", name = "orgId", required = false) @RequestParam(name = "orgId", required = false) String orgId) {
        return ResponseResult.ok(rolePermissionService.queryPermissionListByRoleId(roleId, userType, orgId));
    }

    /**
     * 根据角色id，查询权限列表
     *
     * @param roleId 角色ID
     * @return
     */
    @Operation(summary = "根据角色id，查询权限列表", description = "根据角色id，查询权限列表")
    @GetMapping("/queryPermissionListByRole")
    public ResponseResult<Page<Permission>> queryPermissionListByRole(@Parameter(description = "页码", name = "pageNum", required = true) @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                                                      @Parameter(description = "每页数量", name = "pageSize", required = true) @RequestParam(name = "pageSize", required = true) Integer pageSize,
                                                                      @Parameter(description = "角色ID", name = "roleId", required = true) @RequestParam(name = "roleId", required = true) String roleId) {
        return rolePermissionService.queryPermissionListByRole(pageNum, pageSize, roleId);
    }
}
